package ljye.com.ljye.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import ljye.com.ljye.R;
import ljye.com.ljye.utils.CommonViewHolder;
import me.hwang.widgets.SmartPullableLayout;


/**
 * Created by Administrator on 2016/9/29.
 */
public class Fragment_my_order1 extends Fragment {

    private static final int ON_REFRESH = 1;
    private static final int ON_LOAD_MORE = 2;
    private View view;
    private SmartPullableLayout mPullableLayout;
    private ListView lvContent;
    private ArrayList<String> listData;
    private MyAdpapter mLvAdapter;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case ON_REFRESH:
                    listData.add(0, "新的数据");
                    mLvAdapter.notifyDataSetChanged();
                    mPullableLayout.stopPullBehavior();
                    break;
                case ON_LOAD_MORE:
                    listData.add("加载数据");
                    mLvAdapter.notifyDataSetChanged();
                    mPullableLayout.stopPullBehavior();
                    break;
            }
        }
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null == view) {
            view = inflater.inflate(R.layout.fragment_my_order_base, container, false);
            lvContent = (ListView) view.findViewById(R.id.lv_content);

            listData = new ArrayList<>();
            listData.add("爱迪天才");

            mLvAdapter = new MyAdpapter();
            lvContent.setAdapter(mLvAdapter);

            mPullableLayout = (SmartPullableLayout) view.findViewById(R.id.layout_pullable);

            mPullableLayout.setOnPullListener(new SmartPullableLayout.OnPullListener() {
                @Override
                public void onPullDown() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            mHandler.sendEmptyMessage(ON_REFRESH);
                        }
                    }).start();
                }

                @Override
                public void onPullUp() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            mHandler.sendEmptyMessage(ON_LOAD_MORE);
                        }
                    }).start();
                }
            });
        }
        return view;
    }

    public class MyAdpapter extends BaseAdapter {


        @Override
        public int getCount() {
            return listData == null ? 0 : listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            CommonViewHolder holder = CommonViewHolder.createCVH(view, viewGroup, R.layout.fragment_my_order1);
            TextView uesr_id = holder.getTv(R.id.uesr_id);//用户id
            TextView state = holder.getTv(R.id.state);//状态
            ImageView iv_class = holder.getIv(R.id.class_img);//课程图片
            TextView class_name = holder.getTv(R.id.class_name);//课程名字
            TextView class_hour = holder.getTv(R.id.class_hour);//课时
            TextView class_time = holder.getTv(R.id.class_time);//课程时间
            TextView class_sum = holder.getTv(R.id.money);//课程金额
            TextView time = holder.getTv(R.id.time);//下单时间
            TextView pay = holder.getTv(R.id.pay);//待支付or评价课程


            final String s = listData.get(i);
            uesr_id.setText(s);

            return holder.convertView;
        }
    }

}
